let app = getApp()
Component({
    properties: {
      cart_id:{type:Number},
      buy_page:{type:String},
      buy_type:{type:String},
      buynum:{ 
        type:Number
      },
      messageInfo:{
        type:Object,
        observer: function(newVal, oldVal) {
          this.updatemessage()
        }
      },
      timestamp:{
        type:Number,
        observer: function(newVal, oldVal) {
          this.update()
        }
      },
      spec_id_group: {
        type:String,
        observer: function(newVal, oldVal) {
          this.updatespec_id_group()
        }
      },
      goodsid: {
        type:Number,
        observer: function(newVal, oldVal) {
          this.updateRate()
        }
      }
      },
      data:{  
        moneySign:'¥',
        goCarStatus: 1,
        productInfo: {},
        activityInfo: {},
        buynum:1,
        sku_id:0,
        skuInfo: {
          id: 0,
          label: "",
          price: 0,
          stock: 0,
          image: "",
          spec_id_group: "",
          successFn: null
      }
      },
    lifetimes: {
        attached: function () {
           this.setData({
              globalColor:app.globalData.globalColor
           })
       }
      },
    methods: {
      updatemessage:function(){
        var productInfo=this.data.productInfo
        productInfo['form_setting']=this.data.messageInfo
        this.setData({
          productInfo:productInfo
        })
      },
      updatespec_id_group:function(){},
      close:function(){
        var productInfo=this.data.productInfo
        productInfo['price']='';
        productInfo['show_stock']=false;
        this.setData({
          productInfo:productInfo,
          goCarVisible:false,
          goCarShow:false
        })
      },
      update:function(){
        this.goodsdata(this.data.goodsid)
      },
      goodsdata(id){
        var that=this
        if(this.data.buy_type=='integral')
        {
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Integralmall_getIntegralGoods',
              id:id
            },
            success: function(res) {
                that.setData({
                  goCarVisible:true,
                  goCarShow:true,
                  productInfo:res.data.data.goods,
                  activityInfo:res.data.data.activity,
                  new_goods_id:res.data.data.activity.goods_id
                })
                var spec=[];
                var spec_relate=res.data.data.spec_relate
                for (var i = 0; i <spec_relate.length; i++) {
                  var checked=0;
                  var createArr=[];
                  var spec_id=spec_relate[i]['spec_id'];
                  var p=0;
                  var spec_id_group2=spec_relate[i]['spec_id']+':'+spec_relate[i]['value_id']
                  if(typeof(that.data.spec_id_group)!='undefined'&&that.data.spec_id_group!=''){
                    var array = that.data.spec_id_group.split(";"); 
                    for(var a=0;a<array.length; a++){
                       if(spec_id_group2==array[a])
                        {
                          checked=spec_relate[i]['value_id']
                        }
                    }
                   }
                  for(var s=0;s<spec_relate.length; s++)
                  {
                    var spec_id2=spec_relate[s]['spec_id'];
                    if(spec_id==spec_id2)
                    {
                      if(createArr.length==0&&checked==0)
                      {
                        checked=spec_relate[s]['value_id']
                       
                      }
                      var chiobj = {
                        label:spec_relate[s]['spec_value_name'],
                        value:spec_relate[s]['value_id'] 
                       }
                       createArr.push(chiobj); 
                    }
                  }
                  if(i>0)
                  {
                     if(spec_relate[i]['spec_id']!=spec_relate[i-1]['spec_id'])
                     {
                          p=p+1;
                          var obj = {
                            checked:checked,
                            label:spec_relate[i]['spec_name'],
                            value:spec_relate[i]['spec_id'],
                            children:createArr
                           }
                            spec.push(obj);
                     }
                  }
                  else
                  {
                    var obj = {
                      checked:checked,
                      label:spec_relate[i]['spec_name'],
                      value:spec_relate[i]['spec_id'],
                      children:createArr
                     } 
                      spec.push(obj);
                   }
              }
              that.setData({
                specList:spec,
                goCarVisible:true,
                goCarShow:true,
              })
              that.get_specdata();
            }
          })
        }
        else if(this.data.buy_type=='group'||this.data.buy_type=='group2')
        {
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Group_getGroup',
              id:id
            },
            success: function(res) {
                that.setData({
                  activityInfo:res.data.data.activity,
                  new_goods_id:res.data.data.activity.goods_id
                })
                var productInfo=res.data.data.goods
                if(that.data.buy_type=='group2'){
                  productInfo['price']=res.data.data.activity.activity_price
                }
                that.setData({
                  productInfo:productInfo
                })
                var spec=[];
                var spec_relate=res.data.data.spec_relate
                for (var i = 0; i <spec_relate.length; i++) {
                  var checked=0;
                  var createArr=[];
                  var spec_id=spec_relate[i]['spec_id'];
                  var p=0;
                  var spec_id_group2=spec_relate[i]['spec_id']+':'+spec_relate[i]['value_id']
                  if(typeof(that.data.spec_id_group)!='undefined'&&that.data.spec_id_group!=''){
                    var array = that.data.spec_id_group.split(";"); 
                    for(var a=0;a<array.length; a++){
                       if(spec_id_group2==array[a])
                        {
                          checked=spec_relate[i]['value_id']
                        }
                    }
                   }
                  for(var s=0;s<spec_relate.length; s++)
                  {
                    var spec_id2=spec_relate[s]['spec_id'];
                    if(spec_id==spec_id2)
                    {
                      if(createArr.length==0&&checked==0)
                      {
                        checked=spec_relate[s]['value_id']
                       
                      }
                      var chiobj = {
                        label:spec_relate[s]['spec_value_name'],
                        value:spec_relate[s]['value_id'] 
                       }
                       createArr.push(chiobj); 
                    }
                  }
                  if(i>0)
                  {
                     if(spec_relate[i]['spec_id']!=spec_relate[i-1]['spec_id'])
                     {
                          p=p+1;
                          var obj = {
                            checked:checked,
                            label:spec_relate[i]['spec_name'],
                            value:spec_relate[i]['spec_id'],
                            children:createArr
                           }
                            spec.push(obj);
                     }
                  }
                  else
                  {
                    var obj = {
                      checked:checked,
                      label:spec_relate[i]['spec_name'],
                      value:spec_relate[i]['spec_id'],
                      children:createArr
                     } 
                      spec.push(obj);
                   }
              }
              that.setData({
                specList:spec,
                goCarVisible:true,
                goCarShow:true,
              })
                that.get_specdata();
            }
          })
        } 
        else if(this.data.buy_type=='bargain')
        {
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Bargain_getBargain',
              id:id,
              user_id:wx.getStorageSync('user_id')
            },
            success: function(res) {
              var productInfo = {
                goods_name:res.data.data.goods.goods_name,
                pic:res.data.data.goods.pic,
                price:res.data.data.activity.bottom_price,
                goodsid:res.data.data.goods.id
               } 
               that.setData({
                productInfo:productInfo
               })
               var spec=[];
               var spec_relate=res.data.data.spec_relate
               for (var i = 0; i <spec_relate.length; i++) {
                 var checked=0;
                 var createArr=[];
                 var spec_id=spec_relate[i]['spec_id'];
                 var p=0;
                 var spec_id_group2=spec_relate[i]['spec_id']+':'+spec_relate[i]['value_id']
                 if(typeof(that.data.spec_id_group)!='undefined'&&that.data.spec_id_group!=''){
                   var array = that.data.spec_id_group.split(";"); 
                   for(var a=0;a<array.length; a++){
                      if(spec_id_group2==array[a])
                       {
                         checked=spec_relate[i]['value_id']
                       }
                   }
                  }
                 for(var s=0;s<spec_relate.length; s++)
                 {
                   var spec_id2=spec_relate[s]['spec_id'];
                   if(spec_id==spec_id2)
                   {
                     if(createArr.length==0&&checked==0)
                     {
                       checked=spec_relate[s]['value_id']
                      
                     }
                     var chiobj = {
                       label:spec_relate[s]['spec_value_name'],
                       value:spec_relate[s]['value_id'] 
                      }
                      createArr.push(chiobj); 
                   }
                 }
                 if(i>0)
                 {
                    if(spec_relate[i]['spec_id']!=spec_relate[i-1]['spec_id'])
                    {
                         p=p+1;
                         var obj = {
                           checked:checked,
                           label:spec_relate[i]['spec_name'],
                           value:spec_relate[i]['spec_id'],
                           children:createArr
                          }
                           spec.push(obj);
                    }
                 }
                 else
                 {
                   var obj = {
                     checked:checked,
                     label:spec_relate[i]['spec_name'],
                     value:spec_relate[i]['spec_id'],
                     children:createArr
                    } 
                     spec.push(obj);
                  }
             }
             that.setData({
               specList:spec,
               goCarVisible:true,
               goCarShow:true,
             })
            }
          })
        }else if(this.data.buy_type=='seckillnowbuy')
        {
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Seckill_getSeckill',
              id:id,
              user_id:wx.getStorageSync('user_id')
            },
            success: function(res) {
              var productInfo = {
                goods_name:res.data.data.goods.goods_name,
                pic:res.data.data.goods.pic,
                price:res.data.data.activity.activity_price,
                goodsid:res.data.data.goods.id
               } 
               that.setData({
                productInfo:productInfo,
                activityInfo:res.data.data.activity
               })
               var spec=[];
               var spec_relate=res.data.data.spec_relate
               for (var i = 0; i <spec_relate.length; i++) {
                 var checked=0;
                 var createArr=[];
                 var spec_id=spec_relate[i]['spec_id'];
                 var p=0;
                 var spec_id_group2=spec_relate[i]['spec_id']+':'+spec_relate[i]['value_id']
                 if(typeof(that.data.spec_id_group)!='undefined'&&that.data.spec_id_group!=''){
                   var array = that.data.spec_id_group.split(";"); 
                   for(var a=0;a<array.length; a++){
                      if(spec_id_group2==array[a])
                       {
                         checked=spec_relate[i]['value_id']
                       }
                   }
                  }
                 for(var s=0;s<spec_relate.length; s++)
                 {
                   var spec_id2=spec_relate[s]['spec_id'];
                   if(spec_id==spec_id2)
                   {
                     if(createArr.length==0&&checked==0)
                     {
                       checked=spec_relate[s]['value_id']
                      
                     }
                     var chiobj = {
                       label:spec_relate[s]['spec_value_name'],
                       value:spec_relate[s]['value_id'] 
                      }
                      createArr.push(chiobj); 
                   }
                 }
                 if(i>0)
                 {
                    if(spec_relate[i]['spec_id']!=spec_relate[i-1]['spec_id'])
                    {
                         p=p+1;
                         var obj = {
                           checked:checked,
                           label:spec_relate[i]['spec_name'],
                           value:spec_relate[i]['spec_id'],
                           children:createArr
                          }
                           spec.push(obj);
                    }
                 }
                 else
                 {
                   var obj = {
                     checked:checked,
                     label:spec_relate[i]['spec_name'],
                     value:spec_relate[i]['spec_id'],
                     children:createArr
                    } 
                     spec.push(obj);
                  }
             }
             that.setData({
               goCarVisible:true,
               goCarShow:true,
               specList:spec
             })

            }
          })
        }else{
          wx.request({
            url: app.globalData.siteroot,
            data: {
              do:'Goods_getGoodsInfo',
              goods_id:id
            },
            success: function(res) {
                if(res.data.code==301){
                  that.setData({
                    goCarVisible:false,
                    goCarShow:false
                  })
                  wx.showModal({
                    content:res.data.msg,
                    showCancel:false,
                    success: function (res) {
                    }
                  })               
                  return false
                }
                var spec=[];
                var spec_relate=res.data.data.spec_relate
                for (var i = 0; i <spec_relate.length; i++) {
                  var checked=0;
                  var createArr=[];
                  var spec_id=spec_relate[i]['spec_id'];
                  var p=0;
                  var spec_id_group2=spec_relate[i]['spec_id']+':'+spec_relate[i]['value_id']
                  if(typeof(that.data.spec_id_group)!='undefined'&&that.data.spec_id_group!=''){
                    var array = that.data.spec_id_group.split(";"); 
                    for(var a=0;a<array.length; a++){
                       if(spec_id_group2==array[a])
                        {
                          checked=spec_relate[i]['value_id']
                        }
                    }
                   }
                  for(var s=0;s<spec_relate.length; s++)
                  {
                    var spec_id2=spec_relate[s]['spec_id'];
                    if(spec_id==spec_id2)
                    {
                      if(createArr.length==0&&checked==0)
                      {
                        checked=spec_relate[s]['value_id']
                       
                      }
                      var chiobj = {
                        label:spec_relate[s]['spec_value_name'],
                        value:spec_relate[s]['value_id'] 
                       }
                       createArr.push(chiobj); 
                    }
                  }
                  if(i>0)
                  {
                     if(spec_relate[i]['spec_id']!=spec_relate[i-1]['spec_id'])
                     {
                          p=p+1;
                          var obj = {
                            checked:checked,
                            label:spec_relate[i]['spec_name'],
                            value:spec_relate[i]['spec_id'],
                            children:createArr
                           }
                            spec.push(obj);
                     }
                  }
                  else
                  {
                    var obj = {
                      checked:checked,
                      label:spec_relate[i]['spec_name'],
                      value:spec_relate[i]['spec_id'],
                      children:createArr
                     } 
                      spec.push(obj);
                   }
              }
              if(typeof(that.data.spec_id_group)!='undefined'&&that.data.spec_id_group!=''){
                  var array = that.data.spec_id_group.split(";"); 
                  for (var p = 0; p <spec.length; p++) {
                        var children=spec[p].children
                        for (var c = 0; c <children.length; c++) {
                          var spec_id_group2=spec[p].value+':'+children[c].value
                          if(spec_id_group2==array[p])
                          {
                            spec[p].checked=children[c].value
                          }
                        }
                  }
                  that.setData({
                    goCarVisible:true,
                    goCarShow:true,
                    specList:spec,
                    productInfo:res.data.data.goods
                  })
              }else{
                that.setData({
                  goCarVisible:true,
                  goCarShow:true,
                  specList:spec,
                  productInfo:res.data.data.goods
                })
              }
                that.get_specdata();
            }
          })
        }
       
         
      },
      updateRate:function(){
       
      },
      get_num:function(e){
        this.setData({
          buynum:e.detail
        })
      },
      get_spec:function(e){
        var newspec=this.data.specList;
        var spec_id_group=0;
        newspec[e.detail.index]['checked']=e.detail.value
        this.setData({
          specList:newspec
        })
        this.get_specdata();
      },
      check_message:function(productInfo){
        if(typeof(productInfo.form_setting)!=='undefined'){
          var form_setting=this.data.productInfo.form_setting
          for(var i=0;i<form_setting.length; i++){
            if(form_setting[i]['required']==1&&form_setting[i]['show']==1){
              if(typeof(form_setting[i]['value'])=='undefined'||form_setting[i]['value']==''){
                if(form_setting[i]['type']=='text'||form_setting[i]['type']=='textarea'||form_setting[i]['type']=='phone'||form_setting[i]['type']=='id_card'||form_setting[i]['type']=='car_number'){
                  return false
                }
                if(form_setting[i]['type']=='radio'||form_setting[i]['type']=='checkbox'){
                  return false
                }
                if(form_setting[i]['type']=='file'){
                  return false
                }
                if(form_setting[i]['type']=='date'){
                  return false
                }
                if(form_setting[i]['type']=='datetime'){
                  return false
                }
              }
              if(form_setting[i]['type']=='phone'){
                var myreg=/^1\d{10}$/
                 if (!myreg.test(form_setting[i]['value'])) {
                   return false
                 }
               }
               if(form_setting[i]['type']=='id_card'){
                var myreg = /^[1-9]\d{5}(18|19|20|(3\d))\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
                 if (!myreg.test(form_setting[i]['value'])) {
                   return false
                 }
               }  
            }
          }
          return true
        }
      },
      add_buy:function(){
        var min_buy=this.data.productInfo.min_buy
        var max_buy=this.data.productInfo.max_buy
        if(parseInt(min_buy)>parseInt(this.data.buynum)){
          if(parseInt(min_buy)>0){
            wx.showToast({
              title: '商品最少限购'+min_buy+'件',
              icon: 'none',
              duration: 1500
            })
            return false
          }
        }
        if(parseInt(this.data.buynum)>parseInt(max_buy)){
          if(parseInt(max_buy)>0){
            wx.showToast({
              title: '商品最多购买'+max_buy+'件',
              icon: 'none',
              duration: 1500
            })
            return false
          }
        }
        if(this.data.productInfo.stock==0){
          wx.showToast({
            title: '商品存库不足',
            icon: 'none',
            duration: 1500
          })
            return false
        }
        var that=this
        if(that.data.buy_type=='nowbuy')
        { 
          that.setData({
            goCarVisible:false,
            goCarShow:false
          }) 
          var order=[];
          this.data.productInfo['count']=this.data.buynum
          order.push(this.data.productInfo)
          that.triggerEvent ('nowbuy',order)
        }else{
          if(that.data.buy_page=='car')
          {
            wx.request({
              url: app.globalData.siteroot,
              data: {
                do:"Goods_updateCart",
                user_id:wx.getStorageSync('user_id'),
                cart_id:that.data.cart_id,
                num:that.data.buynum,
                sku_id:that.data.sku_id
              },
              success: function(res) {
                wx.showToast({
                  title: '更新成功！',
                  icon: 'success',
                  duration: 1500
                })
                that.setData({
                  goCarVisible:false,
                  goCarShow:false
                }) 
                that.triggerEvent ('changecacrt',res.data.data.cart_count)
              }
            })
          }else{
            if(that.data.buy_type=='integral')
            {
                var integral=wx.getStorageSync('userinfo').integral
                if(integral<this.data.activityInfo.integral){
                  wx.showToast({
                    title: "您的积分不足，当前积分"+integral,
                    icon: "none"
                  }) 
                }else{
                    var order=[];
                    this.data.productInfo['count']=this.data.buynum
                    this.data.productInfo['activity']='integral'
                    this.data.productInfo['hideCount']=true
                    this.data.productInfo['integral']=this.data.activityInfo.integral
                    this.data.productInfo['activity_id']=this.data.goodsid
                    order.push(this.data.productInfo)
                    this.setData({
                      goCarVisible:false,
                      goCarShow:false
                    }) 
                    wx.setStorageSync('confirmorder',order)
                    wx.navigateTo({ 
                      url: '/yb_shopv2/native/all/index?type=confirm_order&type_id=0&page_type=integral',
                   })
                }
            }
            else if(that.data.buy_type=='group'){
              var order=[];
              this.data.productInfo['count']=this.data.buynum
              this.data.productInfo['activity']='group'
              this.data.productInfo['activity_id']=this.data.goodsid
              order.push(this.data.productInfo)
              wx.setStorageSync('confirmorder',order)
              wx.navigateTo({ 
                url: '/yb_shopv2/native/all/index?type=confirm_order&type_id=0',
             })
            } else if(that.data.buy_type=='group2'){
              var order=[];
              this.data.productInfo['count']=this.data.buynum
              this.data.productInfo['activity']='group2'
              this.data.productInfo['hideCount']=true
              this.data.productInfo['form_setting']=[]
              this.data.productInfo['activity_id']=this.data.goodsid
              order.push(this.data.productInfo)
              wx.setStorageSync('confirmorder',order)
              wx.navigateTo({ 
                url: '/yb_shopv2/native/all/index?type=confirm_order&type_id=0&page_type=group2',
             })
            }else if(that.data.buy_type=='bargain'){
              var goods = [{
                num:this.data.buynum,
                template_id:0,
                sku_id:0,
                goods_id:this.data.productInfo.goodsid
               }]

               wx.request({
                url: app.globalData.siteroot,
                data: {
                  do:"Bargain_createBargain",
                  activity_id:that.data.goodsid, 
                  user_id:wx.getStorageSync('user_id'),
                  goods:goods,
                },
                success: function(res) {
                  that.setData({
                    goCarVisible:false,
                    goCarShow:false
                  })
                  wx.navigateTo({ 
                    url: '/yb_shopv2/native/all/index?type=bargain_detail&activity_id='+that.data.goodsid+'&orderid='+res.data.data.launch_id
                 }) 
                }
              }) 
            }else if(that.data.buy_type=='seckillnowbuy'){
              var order=[];
              this.data.productInfo['count']=this.data.buynum
              this.data.productInfo['activity']='seckill'
              this.data.productInfo['hideCount']=true
              this.data.productInfo['form_setting']=[] 
              this.data.productInfo['activity_id']=this.data.goodsid
              order.push(this.data.productInfo)
              wx.setStorageSync('confirmorder',order)
              wx.navigateTo({ 
                url: '/yb_shopv2/native/all/index?type=confirm_order&type_id=0&page_type=seckill',
             })
            }
            else{
              var goodsid=that.data.goodsid
              if(that.data.buy_type=='group'||that.data.buy_type=='group2'){
                goodsid=that.data.new_goods_id
              }
              wx.request({
                url: app.globalData.siteroot,
                data: {
                  do:"Goods_addCart",
                  goods_id:goodsid, 
                  user_id:wx.getStorageSync('user_id'),
                  sku_id: that.data.sku_id,
                  num: that.data.buynum,
                  goods_name:that.data.productInfo.goods_name,
                  goods_images:that.data.productInfo.pic
                },
                success: function(res) {
                  wx.showToast({
                    title: '商品加入购物车！',
                    icon: 'success',
                    duration: 1500
                  })
                  that.setData({
                    goCarVisible:false,
                    goCarShow:false
                  }) 
                  that.triggerEvent ('changecacrt',res.data.data.cart_count)
                }
              })
            }
          }
         
        }
      },
      add_cart:function(){
        if(typeof(this.data.productInfo.form_setting)!=='undefined'){
              if(this.check_message(this.data.productInfo)){
                  this.add_buy();
              }else{
                wx.showToast({
                  title:'请填写完整信息',
                  icon: 'none',
                  duration: 1500
                })
                this.triggerEvent('editmessage',this.data.productInfo.form_setting)
                return false
              }
        }else{
          this.add_buy();
        }
      },
      get_specdata:function(){
        var spec_id_group=0;
        if(typeof(this.data.specList)!='undefined'){
            var newspec=this.data.specList;
            for(var s=0;s<newspec.length; s++){
              if(spec_id_group==0)
              {
                spec_id_group=newspec[s]['value']+':'+newspec[s]['checked']
              }
              else 
              {
                spec_id_group=spec_id_group+';'+newspec[s]['value']+':'+newspec[s]['checked'];
              }
           }
         }
       var that=this
       wx.request({
           url: app.globalData.siteroot,
           data: {
             do:'Goods_getGoodsSkuAttr',
             goods_id:that.data.goodsid,
             spec_id_group:spec_id_group,
             user_id:wx.getStorageSync('user_id')
           },
           success: function(res) {
             var productInfo=that.data.productInfo
             if(res.data.data.id!=''&&typeof(res.data.data.id)!='undefined')
             {
              if(res.data.data.sku_img==''){
                productInfo['pic']= wx.getStorageSync('thumbimg')
              }else{
                productInfo['pic']=res.data.data.sku_img
              }
              productInfo['price']=res.data.data.price
              productInfo['stock']=res.data.data.stock  
              productInfo['pay_count']=res.data.data.stock
              productInfo['spec_name_group']=res.data.data.spec_name_group
              productInfo['sku_id']=res.data.data.id
              that.setData({
               sku_id:res.data.data.id,
               productInfo:productInfo
              })
             }
             else
             {
              productInfo['sku_id']=0
              that.setData({
                productInfo:productInfo
               })
             }
           }
         })
      }
    }
});
